package com.thp.bigdata.secondarySort;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

/**
 * 订单
 * @author 汤小萌
 *
 */
public class OrderBean implements WritableComparable<OrderBean>{
	private Text itemId;		// 订单id
	private DoubleWritable mount;	// 订单数量
	
	public OrderBean() {}
	
	public OrderBean(Text itemId, DoubleWritable mount) {
		set(itemId, mount);
	}
	public void set(Text itemId, DoubleWritable mount) {
		this.itemId = itemId;
		this.mount = mount;
	}
	
	public Text getItemId() {
		return itemId;
	}
	public void setItemId(Text itemId) {
		this.itemId = itemId;
	}
	public DoubleWritable getMount() {
		return mount;
	}
	public void setMount(DoubleWritable mount) {
		this.mount = mount;
	}
	
	
	
	
	@Override
	public String toString() {
		return itemId + "\t" + mount.get();
	}

	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(itemId.toString());
		out.writeDouble(mount.get());
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		this.itemId = new Text(in.readUTF());
		this.mount = new DoubleWritable(in.readDouble());
	}
	
	// 【注意：】
	//  这个方法是进行排序的
	/**
	 * 在内存往外溢出的时候需要调用比较方法进行排序
	 * 在文件进行合并  merge  的时候也需要调用比较方法进行排序
	 */
	@Override
	public int compareTo(OrderBean o) {
		int cmp = this.itemId.compareTo(o.getItemId());
		if(cmp == 0) {
			// 加上了  -  号  就变成了倒序排序了  从大往小排序
			cmp = -this.mount.compareTo(o.mount);
		}
		return cmp;
	}
	
}
